package com.panopset.app;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.panopset.Util;
import com.panopset.compat.Strings;
import com.panopset.sf.ComponentFactory;
import com.panopset.sf.FrameDefaults;
import com.panopset.sf.PopupBox;
import com.panopset.sf.SwingFrame;
import com.panopset.sf.TitledTextArea;
import com.panopset.sf.Windoc;

/**
 * Swing front-end to URLencoder.
 *
 * @author Karl Dinwiddie
 *
 */
public final class URLcoderPanel {

    /**
     * Test URLcoder.
     *
     * @param s
     *            Not used.
     */
    public static void main(final String... s) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        new FrameDefaults().reset(f);
        f.add(new URLcoderPanel().getPanel());
        f.setVisible(true);
    }

    /**
     * Main panel.
     */
    private JPanel panel;

    /**
     *
     * @return Main panel.
     */
    public JPanel getPanel() {
        if (panel == null) {
            panel = cf.createPanel(new BorderLayout());
        }
        return panel;
    }

    /**
     * Update settings.
     */
    private void updateSettings() {
        getFromTA().getTextArea().setLineWrap(getWrapBox().isSelected());
    }

    /**
     * Wrap text option.
     */
    private JCheckBox wrapBox;

    /**
     *
     * @return Wrap text option.
     */
    private JCheckBox getWrapBox() {
        if (wrapBox == null) {
            wrapBox = cf.createGroundedCheckBox(windoc, "Wrap",
                    "Wrap text", true, "wt");
            wrapBox.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(final ActionEvent e) {
                    updateSettings();
                }
            });
        }
        return wrapBox;
    }

    /**
     * Windoc.
     */
    private Windoc windoc;
    private final ComponentFactory cf = new ComponentFactory();

    /**
     * From text area.
     */
    private TitledTextArea fromTA;

    /**
     * @return From text area.
     */
    public TitledTextArea getFromTA() {
        if (fromTA == null) {
            fromTA = cf.createTitledTextArea(windoc, "Text",
                    "Text that you want encoded or decoded.", "from");
            fromTA.getTextArea().setLineWrap(true);
        }
        return fromTA;
    }

    public URLcoderPanel() {
    	this(null);
    }
    /**
     * @param appWindoc
     *            Application windoc.
     */
    public URLcoderPanel(final Windoc appWindoc) {
        windoc = appWindoc;
        JPanel np = cf.createPanel(new FlowLayout());
        JPanel cp = cf.createPanel(new GridLayout(1, 1));
        JButton btnEncode = new JButton("URL Encode");
        JButton btnDecode = new JButton("URL Decode");
        btnEncode.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                try {
                    getFromTA().setText(
                            URLEncoder.encode(getFromTA().getText(),
                                    Strings.UTF_8));
                } catch (UnsupportedEncodingException err) {
                    Util.log(err);
                }
            }

        });
        btnDecode.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                try {
                    getFromTA().setText(
                            URLDecoder.decode(getFromTA().getText(),
                                    Strings.UTF_8));
                } catch (UnsupportedEncodingException err) {
                    Util.log(err);
                }
            }

        });

        cf.registerPassiveComponent(btnEncode);
        cf.registerPassiveComponent(btnDecode);
        np.add(btnEncode);
        np.add(btnDecode);
        np.add(getWrapBox());
        getPanel().add(BorderLayout.NORTH, np);
        cp.add(getFromTA().getPanel());
        getPanel().add(BorderLayout.CENTER, cp);
    }
}
